package com.example.llh.zyfypt_110llh.fragment.video;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.llh.zyfypt_110llh.R;
import com.example.llh.zyfypt_110llh.adapter.video.VideoAdapter;
import com.example.llh.zyfypt_110llh.bean.log.SessionIDBean;
import com.example.llh.zyfypt_110llh.bean.video.VideoBean;
import com.example.llh.zyfypt_110llh.iface.video.VideoListener;
import com.example.llh.zyfypt_110llh.model.video.VideoModel;

import java.util.List;

public class VideoFragment1 extends Fragment  {
    private RecyclerView recyclerView;//RecyclerView 控件
    private LinearLayoutManager linearLayoutManager;//RecyclerView控件布局
    private VideoAdapter allAdapter;//RecyclerView
    private List<VideoBean> list = null;
    private SessionIDBean sessionIDBean;
    private View view=null;
    private Context context;
    private int page=1; // 代表页数，并初始化为1，代表第1页。
    private int lastVisibleItemPosition;//最后一条可见条目的位置


    VideoListener allListener=new VideoListener() {
        @Override
        public void onResponse(List<VideoBean> beanList) {
            if(page==1)
            {
                list=beanList;
            }
            else {
                list.removeAll(beanList);
                list.addAll(beanList);
            }
            allAdapter.setList(list);//传给adapter
            allAdapter.notifyDataSetChanged();//通知更新
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getActivity(),"失败："+msg, Toast.LENGTH_SHORT);
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
        //动态加载Fragment1的布局文件
        view=inflater.inflate(R.layout.fragment_all,container,false);
        initRecyclerView();
        VideoModel model=new VideoModel();
        model.getResultList("video",page,sessionIDBean.getSessionIDBean,allListener);
        return view;
    }
    private void initRecyclerView(){
        recyclerView = (RecyclerView)view.findViewById(R.id.recycleView_all);
        //创建默认线性布局
        linearLayoutManager= new LinearLayoutManager(getActivity());
        //设置布局管理器
        recyclerView.setLayoutManager(linearLayoutManager);
        //固定每个item高度，可以提高性能
        recyclerView.setHasFixedSize(true);
        //获取数据
        // list=getData();
        //实例化Adapter
        allAdapter= new VideoAdapter(getActivity());
        //传进数据
        allAdapter.setList(list);
        //绑定RecyclerView和Adaper
        recyclerView.setAdapter(allAdapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    page += 1;
                    //再次实例化ArticleModel，调用方法获取网络数据，请求新一页数据
                    VideoModel articleModel = new VideoModel();
                    articleModel.getResultList("video",page,sessionIDBean.getSessionIDBean,allListener);
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=linearLayoutManager.findLastVisibleItemPosition();//滚动结束后将赋值为可见条目中最后一条位置
            }
        });
    }
}
